package scu.debate.web.controller;

import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import scu.debate.dao.entity.User;
import scu.debate.service.user.CookieService;
import scu.debate.service.user.UserService;
import scu.debate.web.utils.InterUtil;
import scu.debate.web.utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private CookieService cookieService;

    private final String COOKIE_NAME = "token";

    @RequestMapping(value = "checkState", method = RequestMethod.GET)
    public String checkState(HttpServletRequest request) {
        return cookieService.checkState(request);
    }
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(@RequestBody Map<String,String> map,HttpServletRequest request, HttpServletResponse response) {
        String account = map.get("account");
        String password = map.get("password");
        String log = userService.login(account, password);
        if (!"success".equals(log)) {
            return ResultUtils.errorMessage("success");
        }
        cookieService.addUser(account, response);
        return ResultUtils.successMessage("login success!");
    }
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public String logout(HttpServletRequest request) {
        cookieService.userLogout(request);
        return  ResultUtils.successMessage("logout!");
    }

    @RequestMapping(value = "sendEmail",method = RequestMethod.POST)
    public String sendEmail(@RequestBody Map<String,String> map){
        String mailAddress = map.get("mailAddress");
        Boolean result = userService.sendMail(mailAddress);
        if (!result){
            return ResultUtils.userErrorMsg("704","邮箱发送失败");
        }
        return ResultUtils.successMessage("验证码已发送");
    }

    @RequestMapping(value = "register",method = RequestMethod.POST)
    public String register(@RequestBody HashMap<String,Object> map) {
        String code = (String) map.get("code");
        User newUser = InterUtil.convertToUser(map);
        String result = userService.addUser(newUser,code);
        if("emailError".equals(result)){
            return ResultUtils.userErrorMsg("702","注册邮箱错误");
        }else if("codeError".equals(result)){
            return ResultUtils.userErrorMsg("703","验证码错误");
        }else if ("repeat".equals(result)){
            return ResultUtils.userErrorMsg("701","账户已被注册");
        }
        userService.deleteCheckCode();
        return ResultUtils.successMessage(result);
    }

    @RequestMapping(value="updateUser",method = RequestMethod.POST)
    public String updateUser(@RequestBody HashMap<String,Object> map){
        User newUser = InterUtil.convertToUser(map);
        String result = userService.updateUser(newUser);
        if ("noUser".equals(result)){
            return ResultUtils.userErrorMsg("721","用户不存在");
        }
        return ResultUtils.successMessage(result);
    }
}
